import { Request, User } from './../../interfaces/request.interface';
import { JwtAuthGuard } from './../../guards/jwt-auth.guard';
import { PaginationQueryDto } from './../../common/dto/paginationQuery.dto';
import { ApiBody, ApiHeader, ApiParam, ApiQuery } from '@nestjs/swagger';
import { ArticleService } from './article.service';
import { Body, Controller, Post, Req, UseGuards, BadRequestException, Get, Param, Query } from '@nestjs/common';
import { CreateArticleDto } from './dto/createArticle.dto';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

@ApiHeader({
    name: 'token'
})
@ApiTags("文章")
@UseGuards(JwtAuthGuard)
@Controller('article')
export class ArticleController {
    constructor(private readonly articleServ: ArticleService) { }

    @ApiOperation({ summary: '文章首页' })
    @Get()
    index(@Query() dto: PaginationQueryDto, @Req() req: Request): any {
        return this.articleServ.getList(dto, req.user.id);
    }

    @ApiParam({ name: 'id', required: true })
    @ApiOperation({ summary: '文章详情' })
    @Get(':id')
    articledetail(@Param('id') id: string, @Req() req: Request): any {
        if (!id) {
            throw new BadRequestException('请传入参数id');
        }
        this.articleServ.queryDetail(id, req.user.id);
    }

    @ApiOperation({ summary: '新增文章' })
    @Post('add')
    add(@Body() createDto: CreateArticleDto, @Req() req: Request): any {
        const user = req.user;
        if (user && user.id) {
            createDto.userId = user.id;
        }
        this.articleServ.createOne(createDto);
    }

    @ApiQuery({ name: 'id' })
    @ApiOperation({ summary: '删除文章' })
    @Post('delete')
    async remove(@Query('id') id: string, @Req() req: Request): Promise<any> {
        if (!id) {
            throw new BadRequestException('请传入参数 id');
        }
        await this.articleServ.deleteById(id);
        return { message: '删除成功！' }
    }

    @ApiQuery({ name: 'id', required: true })
    @ApiOperation({ summary: '文章点赞' })
    @Get('thumb')
    thumb(@Query('id') articleId: string, @Req() req: Request): any {
        if (!articleId) {
            throw new BadRequestException('请传入文章id')
        }
        return this.articleServ.thumb(articleId, req.user.id);
    }
}
